package com.bwie.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.domain.Goods;
import com.bwie.domain.GroupbuyingactivityGoods;
import com.bwie.mapper.GroupbuyingactivityGoodsMapper;
import com.bwie.service.GoodsService;
import com.bwie.mapper.GoodsMapper;
import com.bwie.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author 36167
* @description 针对表【goods(商品表)】的数据库操作Service实现
* @createDate 2025-03-19 10:59:26
*/
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods>
    implements GoodsService{

    @Autowired
    GoodsMapper goodsMapper;
    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Autowired
    GroupbuyingactivityGoodsMapper groupbuyingactivityGoodsMapper;

    @Override
    public R goodslist() {
        List<Goods> goods = goodsMapper.selectList(null);
        return R.OK(goods);
    }

    @Override
    public R goodsadd(Goods goods) {
        String s = RandomUtil.randomString(4);
        goods.setGnum(s);
        int insert = goodsMapper.insert(goods);
        return insert>0?R.OK("添加成功"):R.ERROR(500,"添加失败");
    }

    @Override
    public R getgoods(Integer id) {
       List<Goods> goods= goodsMapper.getgoods(id);
       return R.OK(goods);
    }

    @Override
    public R goodslist1(Integer id) {
       List<Goods> goods= goodsMapper.list(id);
        for (Goods good : goods) {
     GroupbuyingactivityGoods groupbuyingactivityGoods= groupbuyingactivityGoodsMapper.list(good.getId());
      stringRedisTemplate.opsForValue().set(good.getId()+"",groupbuyingactivityGoods.getGroupsize()+"");
        }
        return R.OK(goods);
    }
}




